const {getDmg,reduceResistanceK,lvSuppressK,objValueAdd } = require("../libs/apis.js")

const WEAPONS = require("./weapons.js");

const CLIPDATA_TYPE = {
    max:{
        fixedhp: 299,
        fixedAtt: 19,
        fixdefence: 23,
        reaction : 23,
        attRate: 5.8, 
        hpRate: 5.8, 
        criticalRate: 3.9,
        criticalStrike: 7.8,
        defRate: 7.3,
        charging: 6.5
    },
    mid:{
        fixedhp: 254,   // 209  239   269   299
        fixdefence: 19.75, // 16   19  21  23
        fixedAtt: 16.75,   //  14   16   18  19
        reaction : 19.75,  // 16  19   21   23
        attRate: 4.957,  // 4.1   4.7   5.3  5.8
        hpRate: 4.957,   // 4.1  4.7  5.3  5.8
        criticalRate: 3.3,   //  2.7  3.1   3.5   3.9
        criticalStrike: 6.6,  //  5.4  6.2  7  7.8
        defRate: 6.2,   //  5.1  5.8  6.6   7.3
        charging: 5.5  //  4.5   5.2   5.8   6.5
    }
}

const CLIPDATA = CLIPDATA_TYPE.max

const CONFIG = {
    chance: 25,
    your_lv: 90,
    enemy_lv: 90,
    clips_main: {
        criticalStrike: 62.2,
        dmgRate: 58.3,
        attRate: 46.6
    },
    clips: ["charging", "attRate", "criticalRate", "criticalStrike"],
    clips_initial: [
        ["charging", "attRate", "criticalRate", "criticalStrike"],
        ["charging", "attRate", "criticalRate", "criticalStrike"],
        ["charging", "fixedAtt", "criticalRate", "criticalStrike"],
        ["charging", "attRate", "criticalRate", "criticalStrike"],
        ["charging", "fixedAtt", "attRate", "criticalRate"]
    ],
    reactionK : 1.5,
    reactionDmgAdd: 0,
    buffs: [  
        { dmgRate: 50 , attRate: 18, fixedAtt: 0 },
        { attRate: 20 + 25, fixedAtt: 1202 },
        { attRate: 80 + 20},
    ]
}

let charaters = {
    units : {
        name: "尤拉",
        reaction: 0,
        charging: 100,
        defRate: 0,
        defence: 751,
        hp:13226,
        base: 342,    
        criticalRate: 5,    
        criticalStrike: 88.4,
        skills: {
            a: [
                function(data){},
            ]
        }
    },
}

calc(calc_clips_initial());

function calc(normal) {
    let charactersDmg = [];
    let count = 0;
    Object.values(WEAPONS).forEach(weapon => {
        charactersDmg.push(getDMG([charaters.units,weapon,{fixedAtt:311,...CONFIG.clips_main},normal], (data,second) => {
            count++;
            data = objValueAdd([data, ...CONFIG.buffs]);
            data.hp = data.hp*(1 + (data.hpRate||0)/100) + 4780;
            if(weapon.skill) { weapon.skill(data); }

            let rea = { reaction:data.reaction, reactionQ:CONFIG.reactionK, reactionDmgAdd:CONFIG.reactionDmgAdd }
            // 2.304  3.9168
            let aDmgTest = getDmg({ data, rate: 1.7738}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
            let aDmg = getDmg({ data, rate: 1.7738+1.8493+1.1228*2+2.2267+1.42*2 + 1.7738+1.8493}).qw * reduceResistanceK(-15) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //

            // 2.6352  4.4208 + 2*1.728   ||   3.111 5.219 2*2.04
            let EDmg = getDmg({ data:{...data,dmgRate:0}, rate: 3.111}).qw * reduceResistanceK(-15) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
            let EZhongDmg = getDmg({ data:{...data,dmgRate:0}, rate: 5.219 + 2*2.04}).qw * reduceResistanceK(-15) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
            let EWuDmg = getDmg({ data:{...data,dmgRate:data.dmgRate + 30 + 15}, rate:  9.2227/2}).qw * reduceResistanceK(-70) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //

            //  4.4208  7.2556+1.4824*12    5.219 9.2227 1.8843
            let QBingDmg = getDmg({ data:{...data,dmgRate:0}, rate: 5.219}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
            let QDmg = getDmg({ data:{...data,dmgRate:data.dmgRate + 30 + 25 + 27 + 15}, rate: 9.2227+1.8843*27}).qw * reduceResistanceK(-70) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //

            if(data.phyRate) { // 物理特效伤害
                data.phyDmg = getDmg({data:{...data, dmgRate:0}, rate:data.phyRate}).qw * reduceResistanceK(-15) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);  // 
            }

            let point = (aDmg * 1 + EDmg * 2 + EZhongDmg + QDmg + QBingDmg + EWuDmg + (data.phyDmg||0) * 0) 
            let attack = data.base *(1 + data.attRate/100) + data.fixedAtt;
            return {point, data, second, attack, msg:{aDmg,EDmg,EZhongDmg,EWuDmg,QBingDmg ,QDmg,aDmgTest}}
        }))
    })
    charactersDmg.sort((a,b)=>{
        return (b.point)-(a.point)
    });
    console.log(charactersDmg[28]);
    console.log(charactersDmg.map((v, n)=>{
        return  `(${n})得分: ${v.data.weaponname} : ${v.point.toFixed(0)}`
    }));
    console.log(count);
}

function getDMG(dataarr, getRet){
    let chance = CONFIG.chance;
    let _ret = [];
    for (let index = 0; index <= chance; index++) {
        let c1 = index; 
        for (let index2 = 0; index2 <= chance - index; index2++) {
            let c2 = index2;
            for (let index3 = 0; index3 <= chance-index-index2; index3++) {
                let c3 = index3;
                let c4 = chance - index - index2 - index3;
                let cs = [c1, c2, c3, c4];
                var tmpdata = {};
                CONFIG.clips.forEach((v, i) => {
                    tmpdata[v] = CLIPDATA[v] * cs[i]
                });
                _ret.push(tmpdata);
            }
        }
    }
    _ret = _ret.map(second => {
        let panel = objValueAdd([...dataarr, second]);
        return  getRet(panel, second)
    })
    _ret.sort((b,a)=> a.point-b.point)
    return _ret[0]
}

// 固定攻击力 19 防御力23 生命值299 5.3% 
function calc_clips_initial () {
    let clips = CONFIG.clips_initial.map(v => {
        let tmp = {};
        v.forEach(v2 => {
            tmp[v2] = CLIPDATA[v2]
        })
        return tmp
    })
    let data = {}
    clips.forEach(item => {
        for (const key in item) {
            data[key] = data[key]? (data[key]+item[key]) : item[key]
        }
    })
    return data
}
